<?php

namespace MoChat\ClientNew\Action;

use Carbon\Carbon;
use Hyperf\DbConnection\Db;
use MoChat\ClientNew\Model\ClientFollowRecord;
use MoChat\ClientNew\Model\ClientNew;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Di\Annotation\Inject;
use Qbhy\HyperfAuth\AuthManager;

/**
 * @Controller
 */
class Follow extends AbstractAction
{
    use ValidateSceneTrait;


    protected $auth;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/client-new/follow", methods="get")
     */
    public function handle(): array
    {

        $params = $this->request->all();
        $user =user();
        if (empty($user)) {
            throw new CommonException(ErrorCode::AUTH_UNAUTHORIZED, '登入异常请重新登入');
        }
        $data=ClientFollowRecord::query()->where('client_id',$params['clientId'])->orderBy('id','desc')->paginate()->toArray();
        if (!empty($data['data'])){
            foreach ($data['data'] as &$datum){
                $datum['name']=Db::table('work_contact')->where('id',$datum['workContactId'])->value('name');
            }
            unset($datum);
        }
        return $data;
    }
}
